package com.cstz.ydshop.common.utils;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;

public class OSSUtil {

	private static Logger log = Logger.getLogger(OSSUtil.class);

	private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
	private static String accessKeyId = "<accessKeyId>";
	private static String accessKeySecret = "<accessKeySecret>";
	private static String bucketName = "hebeitoucu";

	static {
		// accessKeyId = PropertyUtils.getAliAccessKey();
		// accessKeySecret = PropertyUtils.getAliAccessKeySecret();
		// bucketName = PropertyUtils.getBucketName();

		accessKeyId = "LTAI4FcwLUWC8WAEUsDRZC5C";
		accessKeySecret = "W88e7i4nSlDLl7deTHbPycD6CyT0OB";
		
	}

	public static void main(String[] args) throws Exception {
		File file = new File("D:/1.txt");
		System.err.println(uploadImage(file,"1.txt"));
	}

	public static String uploadImage(File file, String fileName) throws Exception {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		try {

			ossClient.putObject(new PutObjectRequest(bucketName, fileName, file));
			return "http://"+bucketName+".oss-cn-beijing.aliyuncs.com/"+fileName;
		} catch (OSSException oe) {
			log.error("上传oss图片失败", oe);

			log.error("Error Message: " + oe.getErrorCode());
			log.error("Error Code:       " + oe.getErrorCode());
			log.error("Request ID:      " + oe.getRequestId());
			log.error("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.error("上传oss图片失败", ce);
		} finally {

			ossClient.shutdown();
		}

		return null;
	}

	public static String uploadFile(File file, String fileName) throws Exception {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		try {
			ossClient.putObject(new PutObjectRequest(bucketName, fileName, file));
			return "http://"+bucketName+".oss-cn-beijing.aliyuncs.com/"+fileName;
		} catch (OSSException oe) {
			log.error("上传文件失败", oe);

			log.error("Error Message: " + oe.getErrorCode());
			log.error("Error Code:       " + oe.getErrorCode());
			log.error("Request ID:      " + oe.getRequestId());
			log.error("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.error("上传文件失败", ce);
		} finally {

			ossClient.shutdown();
		}

		return null;
	}

	public static String uploadImage(InputStream in, String fileName) throws Exception {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		try {
			
			ossClient.putObject(new PutObjectRequest(bucketName, fileName, in));
			return "http://"+bucketName+".oss-cn-beijing.aliyuncs.com/"+fileName;
		} catch (OSSException oe) {
			log.error("上传oss图片失败", oe);

			log.error("Error Message: " + oe.getErrorCode());
			log.error("Error Code:       " + oe.getErrorCode());
			log.error("Request ID:      " + oe.getRequestId());
			log.error("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.error("上传oss图片失败", ce);
		} finally {

			ossClient.shutdown();
		}
		
		return null;
	}
	
	public static String uploadImage(String bucket,InputStream in, String fileName) throws Exception {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

		try {
			
			ossClient.putObject(new PutObjectRequest(bucket, fileName, in));
			return "http://"+bucket+".oss-cn-beijing.aliyuncs.com/"+fileName;
		} catch (OSSException oe) {
			log.error("上传oss图片失败", oe);

			log.error("Error Message: " + oe.getErrorCode());
			log.error("Error Code:       " + oe.getErrorCode());
			log.error("Request ID:      " + oe.getRequestId());
			log.error("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			log.error("上传oss图片失败", ce);
		} finally {

			ossClient.shutdown();
		}
		
		return null;
	}
}
